﻿using System;
using System.Collections.Generic;
using System.Data;
using ApplicationLibrary.DataBase;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace ApplicationLibrary.CounterManager
{
    public partial class counterForm : DocumentForm
    {
        private ComboBoxManager cbbManager = null;

        public counterForm()
            : base("counter")
        {
            InitializeComponent();
            InitializeAuxComponent();
        }

        #region Virtual Method

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerCounter("counterForm", new RadarCounter(), this);
            dbManager.AddMaster(this, AM_Counter.Name, true);
            dbManager.AddSlave(AM_CounterValue.Name, true);
            dbManager.AddRelation(AM_CounterValue.Name, AM_Counter.Type, AM_CounterValue.Type);

            dgwValues.AutoGenerateColumns = false;
            dgwValues.DataSource = dbManager.SlaveBinding(AM_CounterValue.Name);
        }

        protected override void OnBindData()
        {
            BindControl(cbbCode, AM_Counter.Type, "SelectedValue");
            BindControl(txtDescription, AM_Counter.Description);
            BindControl(ckbPreEnable, AM_Counter.HasPrefix, "DBChecked");
            BindControl(ckbPreReadonly, AM_Counter.PrefixRO, "DBChecked");
            BindControl(ntbPreLen, AM_Counter.PrefixLen);
            BindControl(txtPreMask, AM_Counter.PrefixMask);
            BindControl(txtPreValue, AM_Counter.PrefixValue);
            BindControl(txtPreSep, AM_Counter.PrefixSep);
            BindControl(cbbPreInit, AM_Counter.PrefixType, "SelectedIndex");

            BindControl(ckbSufEnable, AM_Counter.HasSuffix, "DBChecked");
            BindControl(ckbSufReadonly, AM_Counter.SuffixRO, "DBChecked");
            BindControl(ntbSufLen, AM_Counter.SuffixLen);
            BindControl(txtSufMask, AM_Counter.SuffixMask);
            BindControl(txtSufValue, AM_Counter.SuffixValue);
            BindControl(txtSufSep, AM_Counter.SuffixSep);
            BindControl(cbbSufInit, AM_Counter.SuffixType, "SelectedIndex");

            BindControl(ckbInverted, AM_Counter.InvertSufPref, "Checked");

            BindControl(cbbCodeType, AM_Counter.CodeType, "SelectedIndex");
            BindControl(ntbLength, AM_Counter.CodeLen);
            BindControl(cbbFiscalKey, AM_Counter.CodeKey, "SelectedIndex");

            BindControl(fscExample);
            BindControl(dgwValues);

            colCode.DataPropertyName = AM_CounterValue.Code.Name;
            colValue.DataPropertyName = AM_CounterValue.NumericValue.Name;

            if (dbManager.GetDataColumn(AM_Counter.HasPrefix).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(AM_Counter.HasPrefix).DefaultValue = false;

            if (dbManager.GetDataColumn(AM_Counter.PrefixRO).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(AM_Counter.PrefixRO).DefaultValue = false;

            if (dbManager.GetDataColumn(AM_Counter.PrefixType).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(AM_Counter.PrefixType).DefaultValue = 0;

            if (dbManager.GetDataColumn(AM_Counter.HasSuffix).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(AM_Counter.HasSuffix).DefaultValue = false;

            if (dbManager.GetDataColumn(AM_Counter.SuffixRO).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(AM_Counter.SuffixRO).DefaultValue = false;

            if (dbManager.GetDataColumn(AM_Counter.SuffixType).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(AM_Counter.SuffixType).DefaultValue = 0;

            if (dbManager.GetDataColumn(AM_Counter.CodeKey).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(AM_Counter.CodeKey).DefaultValue = 0;

            if (dbManager.GetDataColumn(AM_Counter.CodeType).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(AM_Counter.CodeType).DefaultValue = 0;

            if (dbManager.GetDataColumn(AM_Counter.Description).DefaultValue is System.DBNull)
                dbManager.GetDataColumn(AM_Counter.Description).DefaultValue = "";

            dgwValues.AddReadOnlyColumn(AM_CounterValue.Code, true);
        }

        protected override void OnDisableControlsForEdit()
        {
            cbbCode.Enabled = false;
            DisableControl();
            base.OnDisableControlsForEdit();
        }

        protected override void OnDisableControlsForNew()
        {
            DisableControl();
        }

        protected override void OnPrepareAuxData()
        {
            ExampleChanged();
            fscExample.Text = "12/1234/";
        }

        protected override void FocusOnNew()
        {
            cbbCode.Focus();
        }

        protected override bool OnBeforeSave()
        {
            return base.OnBeforeSave();
        }

        #endregion

        private void InitializeAuxComponent()
        {
            cbbManager = new ComboBoxManager();
            cbbManager.CreateList<int>();
            foreach (KeyValuePair<int, string> counter in GlobalInfo.CounterTypes)
                cbbManager.AddValue(counter.Key, counter.Value);

            cbbManager.AttachTo(cbbCode);
        }

        private void DisableControl()
        {
            bool hasPrefix = ckbPreEnable.Checked;
            bool isPrefixRO = ckbPreReadonly.Checked;
            PrefixSuffixType PrefixType = (PrefixSuffixType)cbbPreInit.SelectedIndex;
            switch (PrefixType)
            {
                case PrefixSuffixType.ApplicationYear:
                    ntbPreLen.Int = 4;
                    break;

                case PrefixSuffixType.ApplicationYearShort:
                    ntbPreLen.Int = 2;
                    break;
            }
            cbbPreInit.Enabled = hasPrefix;
            ckbPreReadonly.Enabled = hasPrefix;
            txtPreMask.Enabled = hasPrefix && !isPrefixRO && PrefixType == PrefixSuffixType.Custom;
            txtPreSep.Enabled = hasPrefix;
            txtPreValue.Enabled = hasPrefix && PrefixType == PrefixSuffixType.Custom;

            bool hasSuffix = ckbSufEnable.Checked;
            bool isSuffixRO = ckbSufReadonly.Checked;
            PrefixSuffixType SuffixType = (PrefixSuffixType)cbbSufInit.SelectedIndex;
            switch (SuffixType)
            {
                case PrefixSuffixType.ApplicationYear:
                    ntbSufLen.Int = 4;
                    break;

                case PrefixSuffixType.ApplicationYearShort:
                    ntbSufLen.Int = 2;
                    break;
            }
            cbbSufInit.Enabled = hasSuffix;
            ckbSufReadonly.Enabled = hasSuffix;
            txtSufMask.Enabled = hasSuffix && !isSuffixRO && SuffixType == PrefixSuffixType.Custom;
            txtSufSep.Enabled = hasSuffix;
            txtSufValue.Enabled = hasSuffix && SuffixType == PrefixSuffixType.Custom;
        }

        private void ExampleChanged()
        {
            fscExample.PrefixVisible = ckbPreEnable.Checked;
            fscExample.PrefixReadOnly = ckbPreReadonly.Checked;
            fscExample.PrefixMask = txtSufMask.Text;
            fscExample.PrefixMode = (ApplicationLibrary.CounterManager.PrefixSuffixType)cbbPreInit.SelectedIndex;
            fscExample.PrefixSeparator = txtPreSep.Text;
            fscExample.PrefixValue = txtPreValue.Text;

            fscExample.CounterType = (CounterTypes)cbbCodeType.SelectedIndex;
            fscExample.CounterLen = ntbLength.Int;
            fscExample.CounterNumericValue = 0;

            fscExample.SuffixVisible = ckbSufEnable.Checked;
            fscExample.SuffixReadOnly = ckbSufReadonly.Checked;
            fscExample.SuffixMask = txtSufMask.Text;
            fscExample.SuffixMode = (PrefixSuffixType)cbbSufInit.SelectedIndex;
            fscExample.SuffixSeparator = txtSufSep.Text;
            fscExample.SuffixValue = txtSufValue.Text;

            fscExample.Refresh();
        }

        private void ckbPreEnable_CheckStateChanged(object sender, EventArgs e)
        {
            DisableControl();
            ExampleChanged();
        }

        private void cbbPreInit_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisableControl();
            ExampleChanged();
        }

        private void ckbPreReadonly_CheckStateChanged(object sender, EventArgs e)
        {
            DisableControl();
            ExampleChanged();
        }

        private void txtPreMask_TextChanged(object sender, EventArgs e)
        {
            ExampleChanged();
        }

        private void txtPreValue_TextChanged(object sender, EventArgs e)
        {
            ExampleChanged();
        }

        private void txtPreSep_TextChanged(object sender, EventArgs e)
        {
            ExampleChanged();
        }

        private void ckbSufEnable_CheckStateChanged(object sender, EventArgs e)
        {
            DisableControl();
            ExampleChanged();
        }

        private void cbbSufInit_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisableControl();
            ExampleChanged();
        }

        private void ckbSufReadonly_CheckStateChanged(object sender, EventArgs e)
        {
            DisableControl();
            ExampleChanged();
        }

        private void txtSufMask_TextChanged(object sender, EventArgs e)
        {
            ExampleChanged();
        }

        private void txtSufValue_TextChanged(object sender, EventArgs e)
        {
            ExampleChanged();
        }

        private void txtSufSep_TextChanged(object sender, EventArgs e)
        {
            ExampleChanged();
        }

        private void cbbCodeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (cbbCodeType.SelectedIndex == (int)ApplicationLibrary.ExtendedControls.FiscalNoControl.CounterTypes.Text)
            //    ntbLength.RangeMax = 15;
            //else
            //    ntbLength.RangeMax = 8;
            DisableControl();
            ExampleChanged();
        }

        private void txtCodeMask_TextChanged(object sender, EventArgs e)
        {
            DisableControl();
            ExampleChanged();
        }

        private void ntbLength_TextChanged(object sender, EventArgs e)
        {
            DisableControl();
            ExampleChanged();
        }

        private void ntbPreLen_TextChanged(object sender, EventArgs e)
        {
            txtPreMask.MaxLength = ntbPreLen.Int;
            txtPreMask.Text = txtPreMask.Text.Substring(0, Math.Min(ntbPreLen.Int, txtPreMask.TextLength));
            txtPreValue.MaxLength = ntbPreLen.Int;
            txtPreValue.Text = txtPreValue.Text.Substring(0, Math.Min(ntbPreLen.Int, txtPreValue.TextLength));
        }

        private void ntbSufLen_TextChanged(object sender, EventArgs e)
        {
            txtSufMask.MaxLength = ntbSufLen.Int;
            txtSufMask.Text = txtSufMask.Text.Substring(0, Math.Min(ntbSufLen.Int, txtSufMask.TextLength));
            txtSufValue.MaxLength = ntbSufLen.Int;
            txtSufValue.Text = txtSufValue.Text.Substring(0, Math.Min(ntbSufLen.Int, txtSufValue.TextLength));
        }
    }

    #region dbmanagerCounter

    internal class dbmanagerCounter : ApplicationLibrary.DBManager.DBManager
    {
        public dbmanagerCounter(string name, RadarForm radarForm, DocumentForm document)
            : base(name, radarForm, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Insert)
            {
                e.Row[AM_Counter.Year.Name] = GlobalInfo.CurrentDate.Year;
            }
            base.dAdapter_RowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new AM_Counter();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(AM_Counter.Year, "@p1");
            qB.AddCompare(AM_Counter.Type, "@p2");
            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter nParam = new SqlABParameter("@p1", AM_Counter.Year);
            nParam.Value = AM_Counter.Year.DefaultValue;
            PList.Add(nParam);

            SqlABParameter sParam = new SqlABParameter("@p2", AM_Counter.Type);
            sParam.Value = AM_Counter.Type.DefaultValue;
            PList.Add(sParam);
            return PList;
        }

        protected override void SetParameters(IRadarParameters key, DBCollection collection)
        {
            if (collection.Name == AM_Counter.Name)
            {
                collection.Parameter[0].Value = key[0];
                collection.Parameter[1].Value = key[1];
            }
            else
            {
                collection.Parameter[0].Value = key[1];
            }
        }

        protected override string CreateSlaveQuery(string name)
        {
            if (name == AM_CounterValue.Name)
            {
                AM_CounterValue CounterValue = new AM_CounterValue();
                QueryBuilder qB = new QueryBuilder();
                qB.AddSelect();
                qB.AddFrom(AM_CounterValue.Name);
                qB.AddCompare(AM_CounterValue.Type, "@p2");
                qB.AddOrder(AM_CounterValue.Code);
                return qB.Query;
            }

            return "";
        }

        protected override List<SqlABParameter> CreateSlaveParam(string name)
        {
            if (name == AM_CounterValue.Name)
            {
                List<SqlABParameter> PList = new List<SqlABParameter>();

                SqlABParameter sParam = new SqlABParameter("@p2", AM_CounterValue.Code);
                sParam.Value = AM_CounterValue.Code.DefaultValue;
                PList.Add(sParam);
                return PList;
            }
            return null;
        }
    }

    #endregion
}